package net.cardgame.orcalecard.asynctask;

import java.io.InputStream;

import jp.jma.oraclecard.MyApplication;
import net.cardgame.orcalecard.utils.FileUtils;
import uk.co.senab.bitmapcache.BitmapLruCache;
import uk.co.senab.bitmapcache.CacheableBitmapDrawable;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;

public class DecryptBitmapToCache extends AsyncTask<String, Void, Void> {
	private final BitmapLruCache mCache;
	private final BitmapFactory.Options mDecodeOpts;

	public DecryptBitmapToCache(Context context,
			BitmapFactory.Options decodeOpts) {
		mCache = MyApplication.getInstance().getBitmapCache();
		mDecodeOpts = decodeOpts;
	}

	@Override
	protected Void doInBackground(String... params) {
		// TODO Auto-generated method stub
		for (String sourcePath : params) {
			// Now we're not on the main thread we can check all caches
			CacheableBitmapDrawable result = mCache
					.get(sourcePath, mDecodeOpts);
			if (null == result) {
				InputStream is = FileUtils.decrytToInputStream(sourcePath);
				// Add to cache
				result = mCache.put(sourcePath, is, mDecodeOpts, true);
			} else {
				Log.d("ImageUrlAsyncTask", "Got from Cache: " + sourcePath);
			}
		}
		return null;
	}
}